package com.edu.d10_test;
import java.io.*;
import java.net.Socket;
import java.util.Properties;
/**
 * 给服务端一个独立的线程，用来处理客户端发送过来的请求
 */
public class ServerReaderThread3 extends Thread {
    private Socket socket;
    public ServerReaderThread3(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        //使用try/catch来进行捕获异常，防止因socket管道通道关闭而引发的异常
        try {
            //处理的信息分为两部分，注册信息和登录信息
            InputStream inputStream = socket.getInputStream();
            //用数据流进行包装
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            while (true) {
                //读消息类型
                int type = dataInputStream.readInt();
                //区别消息，进行不同的处理
                switch (type) {
                    case 1:
                        //注册
                        register();
                    case 2:
                        //登录
                        login();
                }
            }
        } catch (Exception e) {
            //离线逻辑
            System.out.println("流通道关闭，有人下线了！！");
        }

    }

    private void login() throws Exception {
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        String loginname = dataInputStream.readUTF();
        String password = dataInputStream.readUTF();

        String result = "登录成功！";
        File file = new File(Server.USERS_FILE_PATH);
        if (file.exists()) {
            //提取全部的登录名，看是否存在当前这个登录名
            Properties properties = new Properties();
            properties.load(new FileReader(file));
            //校验登录名
            String OKPwd = properties.getProperty(loginname);
            if (OKPwd == null) {
                result = "登录名称为空";
            } else if (!OKPwd.equals(password)) {
                //校验密码
                result = "密码错误，登录失败！";
            }
        } else {
            result = "当前无任何用户信息，无法登录！";
        }
        //响应结果
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF(result);
        dataOutputStream.flush();
    }

    private void register() throws Exception {
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        String loginName = dataInputStream.readUTF();
        String password = dataInputStream.readUTF();
        //开始判断登录名是否重复
        String result = "注册成功";
        if (exits(loginName)) {
            result = "用户名已存在，注册失败！";
        } else {
            //保存登录名和密码
            Properties properties = new Properties();
            //将登录名和密码放到属性集合当中
            properties.setProperty(loginName, password);
            //追加写入
            properties.store(new FileWriter(Server.USERS_FILE_PATH, true), "");
        }
        //响应回去结果
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF(result);
        dataOutputStream.flush();

    }

    private boolean exits(String loginName) throws Exception {
        //开始判断登录名是否重复
        File file = new File(Server.USERS_FILE_PATH);
        //判断文件是否存在
        if (file.exists()) {
            //提取全部的登录名，看是否存在当前这个登录名
            Properties properties = new Properties();
            properties.load(new FileReader(file));
            if (properties.containsKey(loginName)){
                return true;
            }
        }
        return false;
    }
}
